package com.ct.sales.view.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.ct.sales.R;
import com.up91.common.android.widget.header.HeaderFragment;

public class CustomHeaderFragment extends HeaderFragment {
    private TextView  mTVCenterTitle;
    private TextView  mTVRight;
    private ImageView mIVRightDivider;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup v = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);

        v.setBackgroundResource(R.drawable.bg_common_header);

        inflater.inflate(R.layout.header_bottom_line, getAreaBottom(), true);

        setDefaultLeftView();

        getRightRoot().setMinimumWidth(inflater.getContext().getResources().getDimensionPixelSize(R.dimen.header_side_min_width));

        return v;
    }

    public void setBackground(int resId) {
        getView().setBackgroundResource(resId);
    }

    private void setDefaultLeftView() {
        setLeftView(R.layout.customheader_left);
        ImageButton btnLeft = (ImageButton) getLeftRoot().findViewById(R.id.btn_header_left);
        btnLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
    }

    public void setDefaultLeftListener(OnClickListener listener) {
        getLeftView().findViewById(R.id.btn_header_left).setOnClickListener(listener);
    }

    public void setDefaultRightListener(OnClickListener listener) {
        if(!ensureDefaultRight()) return;
        mTVRight.setOnClickListener(listener);
    }

    public void setLeftView(View view) {
        getLeftRoot().removeAllViews();
        if (null != view) {
            getLeftRoot().addView(view);
        }
    }

    public void setLeftView(int layout) {
        getLeftRoot().removeAllViews();
        getLayoutInflater(null).inflate(layout, getLeftRoot());
    }

    public View getLeftView() {
        return getLeftRoot().getChildAt(0);
    }

    void setRightDividerVisible(boolean visible) {
//        mIVRightDivider.setVisibility(visible?View.VISIBLE:View.INVISIBLE);
    }

    public void setRightView(View view) {
        getRightRoot().removeAllViews();
        if (null != view) {
            getRightRoot().addView(view);
        }
        setRightDividerVisible(true);
    }

    public void setRightView(int layout) {
        getRightRoot().removeAllViews();
        getLayoutInflater(null).inflate(layout, getRightRoot());
        setRightDividerVisible(true);
    }

    public View getRightView() {
        return getRightRoot().getChildAt(0);
    }

    public void setCenterView(View view) {
        getCenterRoot().removeAllViews();
        if (null != view) {
            getCenterRoot().addView(view);
        }
    }

    public void setCenterView(int layout) {
        getCenterRoot().removeAllViews();
        getLayoutInflater(null).inflate(layout, getCenterRoot());
    }

    public View getCenterView() {
        return getCenterRoot().getChildAt(0);
    }

    private boolean ensureDefaultCenter() {
        if (null == mTVCenterTitle && isAdded()) {
            View center = getLayoutInflater(null).inflate(R.layout.customheader_center, null);
            mTVCenterTitle = (TextView) center.findViewById(R.id.tv_header_center);
            setCenterView(center);
        }
        return null != mTVCenterTitle;
    }

    private boolean ensureDefaultRight() {
        if (null == mTVRight && isAdded()) {
            View right = getLayoutInflater(null).inflate(R.layout.customheader_right, null);
            mTVRight = (TextView) right.findViewById(R.id.tv_header_right);
            setRightView(right);
        }
        return null != mTVRight;
    }

    public void setCenterText(CharSequence text) {
        if(!ensureDefaultCenter()) return;
        mTVCenterTitle.setText(text);

        mTVCenterTitle.post(new Runnable() {
            @Override
            public void run() {
                int leftWidth = getLeftRoot().getWidth();
                int rightWidth = getRightRoot().getWidth();
                int sideRest = Math.max(leftWidth, rightWidth);
                mTVCenterTitle.setMaxWidth(getView().getWidth() - 2 * sideRest);
            }
        });
    }

    public void setCenterText(int resId, Object... formatArgs) {
        setCenterText(getActivity().getString(resId, formatArgs));
    }

    public void setRightText(CharSequence text) {
        if(!ensureDefaultRight()) return;
        setRightDividerVisible(true);
        mTVRight.setText(text);
    }

    public void setRightText(int resId, Object... formatArgs) {
        setRightText(getActivity().getString(resId, formatArgs));
    }
}
